package com.wanjia.estate.constant.printinfo;

import lombok.AllArgsConstructor;

import java.util.Objects;
import java.util.function.Supplier;
import java.util.stream.Stream;

/**
 * 打印机状态
 * <p>0 = Other</p>
 * <p>1 = Unknown	 //打印机纸张未放好等</p>
 * <p>2 = Idle	 	//打印机空闲</p>
 * <p>3 = Printing	//打印机正在打印</p>
 * <p>4 = Warming Up	//扫描灯正在加热</p>
 */
@AllArgsConstructor
public enum Status {

    OTHER(0, "Other", null),
    UNKNOWN(1, "Unknown", "打印机纸张未放好等"),
    IDLE(2, "Idle", "打印机空闲"),
    PRINTING(3, "Printing", "打印机正在打印"),
    WARMING_UP(4, "Warming Up", "扫描灯正在加热");


    private final Integer code;
    private final String statusName;
    private final String desc;

    public Integer code() {
        return code;
    }

    public String statusName() {
        return statusName;
    }

    public String desc() {
        return desc;
    }

    public static Status get(Integer code) {
        return Stream.of(values())
                .filter(s -> Objects.equals(code, s.code))
                .findFirst()
                .orElse(null);
    }

    public static Status getOrElse(Integer code, Status orElse) {
        return Stream.of(values())
                .filter(s -> Objects.equals(code, s.code))
                .findFirst()
                .orElse(orElse);
    }

    public static Status getOrElseGet(Integer code, Supplier<Status> supplier) {
        return Stream.of(values())
                .filter(s -> Objects.equals(code, s.code))
                .findFirst()
                .orElseGet(supplier);
    }

    public static <X extends Throwable> Status getOrElseThrow(Integer code, Supplier<? extends X> supplier) throws X {
        return Stream.of(values())
                .filter(s -> Objects.equals(code, s.code))
                .findFirst()
                .orElseThrow(supplier);
    }
}
